import Vue from "vue";
import VueRouter from "vue-router";

import Login from "../views/home/login.vue";

import OrganizationUser from "../views/user/organizationUser.vue";
import IndividualUser from "../views/user/individualUser.vue";

import LevelManager from "../views/exam/levelManager.vue";
import LevelFrom from "../views/exam/levelFrom.vue";
import AreaManager from "../views/exam/areaManager.vue";
import ExamManager from "../views/exam/examManager.vue";
import ExamFrom from "../views/exam/examFrom.vue";
import ExaminerManager from "../views/exam/examinerManager.vue";

import IndividualOrder from "../views/order/individualOrder.vue";
import OrganizationOrder from "../views/order/organizationOrder.vue";
import OrderDetail from "../views/order/orderDetail.vue";

import ExamineeList from "../views/examinee/examineeList.vue";
import ExamineeFrom from "../views/examinee/examineeFrom.vue";

import ResultsList from "../views/results/resultsList.vue";

import IncomeManager from "../views/account/incomeManager.vue";
import RefundManager from "../views/account/refundManager.vue";
import TransactionManager from "../views/account/transactionManager.vue";

import ExamineeImageList from "../views/examinee/examineeImageList.vue";

Vue.use(VueRouter);

const routes = [
  // home
  {
    path: "/",
    name: "Home",
    redirect: "/user/individualUser",
  }, // 首页
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      title: "登录",
    },
  }, // 登录

  // 用户管理
  {
    path: "/user/organizationUser",
    name: "OrganizationUser",
    component: OrganizationUser,
    meta: {
      title: "机构用户",
    },
  }, // 机构用户管理
  {
    path: "/user/individualUser",
    name: "IndividualUser",
    component: IndividualUser,
    meta: {
      title: "个人用户",
    },
  }, // 个人用户管理

  // 考试管理
  {
    path: "/exam/levelManager",
    name: "LevelManager",
    component: LevelManager,
    meta: {
      title: "等级管理",
    },
  }, // 等级管理
  {
    path: "/exam/levelManager/levelFrom",
    name: "LevelFrom",
    component: LevelFrom,
    meta: {
      title: "等级管理",
    },
  }, // 等级新增/编辑
  {
    path: "/exam/areaManager",
    name: "AreaManager",
    component: AreaManager,
    meta: {
      title: "考点管理",
    },
  }, // 考点管理
  {
    path: "/exam/examManager",
    name: "ExamManager",
    component: ExamManager,
    meta: {
      title: "科目管理",
    },
  }, // 科目管理
  {
    path: "/exam/examManager/examFrom",
    name: "ExamFrom",
    component: ExamFrom,
    meta: {
      title: "科目管理",
    },
  }, // 科目新增/编辑
  {
    path: "/exam/examManager/examinerManager",
    name: "ExaminerManager",
    component: ExaminerManager,
    meta: {
      title: "科目管理员管理",
    },
  }, // 科目管理员管理

  // 订单管理
  {
    path: "/order/individualOrder",
    name: "IndividualOrder",
    component: IndividualOrder,
    meta: {
      title: "个人订单",
    },
  }, // 个人订单
  {
    path: "/order/organizationOrder",
    name: "OrganizationOrder",
    component: OrganizationOrder,
    meta: {
      title: "机构订单",
    },
  }, // 机构订单
  {
    path: "/order/orderDetail",
    name: "OrderDetail",
    component: OrderDetail,
    meta: {
      title: "订单详情",
    },
  }, // 订单详情

  // 考生管理
  {
    path: "/examinee/examineeList",
    name: "ExamineeList",
    component: ExamineeList,
    meta: {
      title: "考生列表",
    },
  }, // 考生列表
  {
    path: "/examinee/examineeFrom",
    name: "ExamineeFrom",
    component: ExamineeFrom,
    meta: {
      title: "考生管理",
    },
  }, // 添加/编辑考生

  // 成绩管理
  {
    path: "/results/resultsList",
    name: "ResultsList",
    component: ResultsList,
    meta: {
      title: "成绩管理",
    },
  }, // 成绩管理

  // 账户管理
  {
    path: "/account/incomeManager",
    name: "IncomeManager",
    component: IncomeManager,
    meta: {
      title: "收支信息",
    },
  }, // 收支信息
  {
    path: "/account/refundManager",
    name: "RefundManager",
    component: RefundManager,
    meta: {
      title: "退款申请",
    },
  }, // 退款申请
  {
    path: "/account/transactionManager",
    name: "TransactionManager",
    component: TransactionManager,
    meta: {
      title: "交易记录",
    },
  }, // 交易记录

  {
    path: "/examinee/examineeImageList",
    name: "ExamineeImageList",
    component: ExamineeImageList,
    meta: {
      title: "考生管理",
    },
  }, // 考生图片审查
];

const router = new VueRouter({
  mode: "history",
  base: "/admin/",
  routes,
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 需要登录的页面
  const nextRoute = [
    "Home",

    "OrganizationUser",
    "IndividualUser",

    "LevelManager",
    "LevelFrom",
    "AreaManager",
    "ExamManager",
    "ExamFrom",
    "ExaminerManager",

    "IndividualOrder",
    "OrganizationOrder",
    "OrderDetail",

    "ExamineeList",
    "ExamineeFrom",

    "ResultsList",

    "IncomeManager",
    "RefundManager",
    "TransactionManager",

    "ExamineeImageList",
  ];
  // const nextRoute = [];
  let isLogin = localStorage.getItem("ADMIN_LOGINTOKEN"); // 判断是否登录，本地存储有用户数据则视为已经登录
  // 未登录状态；当路由到 nextRoute 指定页时，跳转至 登录
  if (nextRoute.indexOf(to.name) >= 0) {
    // 检测是否登录的页面
    if (!isLogin) {
      // 如果未登录（本地存储无用户数据），并且要跳到登录页面
      if (from.name === "Login") {
        next("/login");
        return;
      }
      // 登录后，跳到到当前页面
      router.push({
        name: "Login",
      });
    }
  }

  // 已登录状态；当路由到 登录 时，跳转至 首页
  if (to.name === "Login") {
    if (isLogin) {
      next("/");
      return;
    }
  }
  next(); // 必须使用 next ,执行效果依赖 next 方法的调用参数
});
export default router;
